package com.wunu.zn.api.appliance.listener;

import javax.jms.Destination;  
import javax.jms.JMSException;
import javax.jms.Session;  
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.listener.SessionAwareMessageListener;

import com.wunu.zn.api.appliance.service.ActivityMqService;
import com.wunu.zn.api.appliance.service.IDevMessageCached;

import net.sf.json.JSONObject;

public class ConsumerSessionAwareMessageListener implements  
        SessionAwareMessageListener<TextMessage> {
	private static Logger log = Logger.getLogger(SessionAwareMessageListener.class);
	private Destination destination;  
	@Autowired
	private ActivityMqService mqService;
	@Autowired
	private IDevMessageCached devMessageCached;
	/**
	 * 监听硬件端发送数据
	 */
    public void onMessage(TextMessage message, Session session) throws JMSException {  
        int result = 0;
        TextMessage tm = (TextMessage) message;
        JSONObject json =  mqService.getDevMessage(tm.getText()); 
      try {
             result =  mqService.messageListener(json);
            if(result!=1){
            	log.error("ConsumerSessionAwareMessageListener监听到了文本消息保存失败：\t"+ tm.getText());
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
        devMessageCached.set(json.getString("name"), json.get("messages").toString());
        log.info("devMessageCached.get(json.getString('name')):"+devMessageCached.get(json.getString("name")));
     /*   devMessageCached.set(devId, obj);*/
       /* String cid =message.getStringProperty("cid");//回复数据打开代码
    	String sid =message.getStringProperty("sid");
        MessageProducer producer = session.createProducer(destination);  
        Message textMessage =session.createTextMessage("ok"); 
        textMessage.setStringProperty("cid", cid);
        textMessage.setStringProperty("sid", sid);
        producer.send(textMessage);*/
    }  
   
    public Destination getDestination() {  
        return destination;  
    }  
   
    public void setDestination(Destination destination) {  
        this.destination = destination;  
    }  
   
}  